package Jasper.servlet;

import Jasper.entity.Pro_Class;
import Jasper.entity.Product;
import Jasper.entity.Type;
import Jasper.service.IProductService;
import Jasper.service.impl.ProductServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName ProductServlet
 * @Author jiangjiajia
 * @JDK-Version 1.8
 * @Date 2021/6/2 9:16 pm
 * @Version 1.0
 * @Description 必须描述类的业务用途
 **/

@WebServlet("/ProductServlet")
public class ProductServlet extends BaseServlet {
    //接口多态
    private IProductService productService = new ProductServiceImpl();

    /**
     * 商品展示
     * @param request
     * @param response
     * @return
     */
    // 获取请求和响应
    public List<Product> products(HttpServletRequest request, HttpServletResponse response) {
        //获取服务层的方法
        List<Product> list = productService.getproductlist();
        //返回集合
        return list;
    }

    /**
     * 新增加产品分类方法
     * @param request
     * @param response
     * @return
     * @throws FileUploadException
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     * @throws ServletException
     * @throws InvocationTargetException
     */
    public Type addPro_Class(HttpServletRequest request, HttpServletResponse response) throws FileUploadException, IOException, InvocationTargetException, IllegalAccessException, ServletException, InvocationTargetException {
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List<FileItem> list = upload.parseRequest(request);
        Pro_Class pro_class = new Pro_Class();
        HashMap<String, String> map = new HashMap<>();
        for (FileItem fileItem : list) {
            //判断是否为非文本信息
            if (!fileItem.isFormField()) {
                InputStream is = fileItem.getInputStream();
                String path = getServletContext().getRealPath("img");
                File file = new File(path);
                if (!file.exists()) {
                    file.mkdirs();
                }
                //改变文件名
                String fileName = fileItem.getName();
                UUID uuid = UUID.randomUUID();
                fileName = uuid.toString() + "_" + fileName;

                FileOutputStream fos = new FileOutputStream(new File(path, fileName));
                IOUtils.copy(is, fos);
                is.close();
                fos.close();
                //存储图片的名字
//                product.setImgUrl(fileName);
            } else {
                //userName  --  hobbies -- hobbies
                String value = map.get(fileItem.getFieldName());
                if (value == null) {
                    //userName:中文   hobbies:eat
                    map.put(fileItem.getFieldName(), fileItem.getString("utf-8"));
                } else {
                    //hobbies:eat,drink
                    map.put(fileItem.getFieldName(), value + "," + fileItem.getString("utf-8"));
                }
            }
        }
/*        //eat,drink
        String hobbiesString = map.get("hobbies");
        String[] hobbies = hobbiesString.split(",");
        product.setHobbies(hobbies);
        user.setHobbiesString(hobbiesString);*/

        //会自动把map中的value赋值给user对象，前提条件是map的key跟user对象的属性相同
        BeanUtils.populate(pro_class, map);
//        Type type = userService.addProducr(product);
        Type type = productService.addPro_Class(pro_class);
        //把对象成功存放到数据库中
        System.out.println(pro_class);
//        Type type = new Type("success");
        //响应前端
        return type;
    }

    /**
     * 获取商品类别展示
     * @param request
     * @param response
     * @return
     */
    public List<Pro_Class> getPro_class(HttpServletRequest request, HttpServletResponse response){
        List<Pro_Class> list = productService.getPro_classlist();
        return list;
    }
}
